//
//  JJTabBarViewController.m
//
//
//  Created by apple on 14-7-3.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "CLTabBarViewController.h"
#import "AppDelegate.h"
#import "UITabBar+CLBadge.h"
#import "UIImage+Fit.h"

@interface CLTabBarViewController () < UITabBarControllerDelegate>

@end

@implementation CLTabBarViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.delegate = self;
    
    // 添加所有的子控制器
    [self addAllChildVcs];
    
    // 创建自定义tabbar
    [self addCustomTabBar];
    
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UINavigationController *)viewController
{
    UIViewController *vc = [viewController.viewControllers firstObject];
    if ([vc isKindOfClass:[CLHomeViewController class]]) {
        if (self.lastSelectedViewContoller == vc) {
            DYLog(@"选择了控制器CLMsgBoxTVC");

        } else {

        }
    }
    self.lastSelectedViewContoller = vc;
}

/**
 *  创建自定义tabbar
 */
- (void)addCustomTabBar
{
    // 创建自定义tabbar
    CLTabBar *customTabBar = [[CLTabBar alloc] init];
    customTabBar.delegate = self;
    // 更换系统自带的tabbar
    [self setValue:customTabBar forKeyPath:@"tabBar"];
}

/**
 *  添加所有的子控制器
 */
- (void)addAllChildVcs
{
    CLHomeViewController *sportVC = [[CLHomeViewController alloc] init];
    [self addOneChlildVc:sportVC title:CLLocalizedString(@"Sprot", nil) imageName:@"tabbar_gauge" selectedImageName:@"tabbar_gauge_selected"];
    self.sportVC = sportVC;
    self.lastSelectedViewContoller = sportVC;
    
    CLWalletViewController *walletVC = [[CLWalletViewController alloc] init];
    [self addOneChlildVc:walletVC title:CLLocalizedString(@"Wallet", nil) imageName:@"tabbar_recharge" selectedImageName:@"tabbar_recharge_selected"];
    self.walletVC = walletVC;
    
   /* CLDiscoverViewController *discoverVC = [[CLDiscoverViewController alloc] init];
    [self addOneChlildVc:discoverVC title:CLLocalizedString(@"discover", nil) imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
    self.discoverVC = discoverVC;*/
    
    CLProfileTableViewController *me = [[CLProfileTableViewController alloc]init];
    [self addOneChlildVc:me title:CLLocalizedString(@"profile", nil) imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
    self.me = me;
}

/**
 *  添加一个子控制器
 *
 *  @param childVc           子控制器对象
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)addOneChlildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 设置标题
    childVc.title = title;
    
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 设置tabBarItem的普通文字颜色
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 设置tabBarItem的选中文字颜色
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    selectedTextAttrs[NSForegroundColorAttributeName] = DYColor(41, 132, 215);
    [childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
    UIOffset offSet = UIOffsetMake(0, -2);
    childVc.tabBarItem.titlePositionAdjustment = offSet;
    
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    // 声明这张图片用原图(别渲染)
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = selectedImage;
    
    // 添加为tabbar控制器的子控制器
    CLNavigationController *nav = [[CLNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
    //设置导航栏背景颜色
    [childVc.navigationController.navigationBar setBackgroundImage:[UIImage imageWithSize:CGSizeMake(sWidth, 64) FromColor:ThemeColor] forBarMetrics:UIBarMetricsDefault];
    childVc.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
}

/**
 *  刷新tabBar 的红点
 *
 *  @param index 在第几个BadgeOnItem添加红点
 */
- (void)refreshTabBarController:(CLTabBarViewController*)tabBarController TabBadgewithIndex:(int) index{
    /*AppDelegate *theAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    int num = 1;
    if (num> 0){
        //显示红点
        [tabBarController.tabBar showBadgeOnItemIndex:index];
    }else{
        //隐藏红点
        [tabBarController.tabBar hideBadgeOnItemIndex:index];
    }*/
}
@end